package tools

import (
	"encoding/json"
	"fmt"
	cfg2 "gitee.com/wanjimao/dongli-platform/luban/Assets/Gen/Json"
	"gitee.com/wanjimao/dongli-util/helper"
	"io/ioutil"
)

type VarTools struct {
}

var VarToolsObject VarTools

var GlobalRandNameSlice = make([]string, 0)
var GlobalDataMapChannel = make(map[int32]*cfg2.DataChannel, 0)

var GlobalDataMapScene = make(map[int32]*cfg2.DataScene, 0)

var GlobalDataMapItemRule = make(map[string]*cfg2.DataItemRule, 0)

var GlobalDataMapInitInitialClothing = make(map[int32]*cfg2.DataInitialClothing, 0)

var GlobalDataMapInitInitialProps = make(map[int32]*cfg2.DataInitialProps, 0)

func loader(file string) ([]map[string]interface{}, error) {
	filePath := "luban/GenerateDatas/json/" + file + ".json"
	if bytes, err := ioutil.ReadFile(filePath); err != nil {
		return nil, err
	} else {
		jsonData := make([]map[string]interface{}, 0)
		if err = json.Unmarshal(bytes, &jsonData); err != nil {
			return nil, err
		}
		return jsonData, nil
	}
}

func (v VarTools) InitRandName() {
	tables, err := cfg2.NewTables(loader)
	fmt.Println(fmt.Sprintf("table :%+v, err:%+v", tables, err))
	for _, value1 := range tables.TbNickName.GetDataList() {
		if value1.Surname == "" {
			continue
		}
		for _, value2 := range tables.TbNickName.GetDataList() {
			if value2.Name == "" {
				continue
			}
			GlobalRandNameSlice = append(GlobalRandNameSlice, value1.Surname+value2.Name)
		}
	}
	//
	if len(GlobalRandNameSlice) < 100 {
		panic("全局变量随机名字不能低于100")
	}
	fmt.Println(fmt.Sprintf("全局变量随机名字  数量（%+v）  是 %+v", len(GlobalRandNameSlice), GlobalRandNameSlice))

	GlobalDataMapChannel = tables.TbChannel.GetDataMap()

}

func (v VarTools) InitScene() {
	tables, err := cfg2.NewTables(loader)
	fmt.Println(fmt.Sprintf("table :%+v, err:%+v", tables, err))
	//for _, value1 := range tables.TbScene.GetDataList() {
	//	GlobalDataMapScene[value1.Id] = value1
	//}
	////
	//fmt.Println(fmt.Sprintf("全局变量随机名字  数量（%+v）  是 %+v", len(GlobalDataMapScene), GlobalDataMapScene))

	GlobalDataMapScene = tables.TbScene.GetDataMap()
	fmt.Println(fmt.Sprintf("全局变量随机名字  数量（%+v）  是 %+v", len(GlobalDataMapScene), GlobalDataMapScene))

}

func (v VarTools) InitItem() {
	tables, err := cfg2.NewTables(loader)
	fmt.Println(fmt.Sprintf("table :%+v, err:%+v", tables, err))
	for _, value1 := range tables.TbItemRule.GetDataList() {
		GlobalDataMapItemRule[helper.InterfaceHelperObject.ToString(value1.Num1)+"和"+helper.InterfaceHelperObject.ToString(value1.Num2)] = value1
	}
	fmt.Println(fmt.Sprintf("全局变量随机名字 GlobalDataMapItem 数量（%+v）  是 %+v", len(GlobalDataMapItemRule), GlobalDataMapItemRule))

}

func (v VarTools) InitInitialClothing() {
	tables, _ := cfg2.NewTables(loader)
	GlobalDataMapInitInitialClothing = tables.TbInitialClothing.GetDataMap()
	fmt.Println(fmt.Sprintf("全局变量随机名字 GlobalDataMapItem 数量（%+v）  是 %+v", len(GlobalDataMapInitInitialClothing), GlobalDataMapInitInitialClothing))
}

func (v VarTools) InitInitialProps() {
	tables, _ := cfg2.NewTables(loader)
	GlobalDataMapInitInitialProps = tables.TbInitialProps.GetDataMap()
	fmt.Println(fmt.Sprintf("全局变量随机名字 GlobalDataMapItem 数量（%+v）  是 %+v", len(GlobalDataMapInitInitialProps), GlobalDataMapInitInitialProps))
}
